﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using Microsoft.Phone.BackgroundAudio;
using System.IO.IsolatedStorage;
using System.IO;
namespace TruyenAudio
{
    public partial class MainPage : PhoneApplicationPage
    {
        // Constructor
        public MainPage()
        {
            InitializeComponent();

            IsolatedStorageFile store = IsolatedStorageFile.GetUserStoreForApplication();
            if (!store.FileExists("CSDLTruyen.sdf"))
            {
                MoveReferenceDatabase();
            }

            //to mau cho chu khi item da duoc chon
            //foreach (Item item in lstData.Items)
            //{
            //    if (item.ProductName.Equals(ListItem.TheLoai))
            //    {
            //    }
            //}
        }

        private void lstData_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            Item item = (Item)lstData.SelectedItem;
            ListItem.TheLoai = item.ProductName;
            ((App)Application.Current).RootFrame.Navigate(new Uri("/DanhSachTruyen.xaml", UriKind.Relative));
        }

        protected override void OnBackKeyPress(System.ComponentModel.CancelEventArgs e)
        {
            MessageBoxResult result = MessageBox.Show("Bạn thật sự muốn thoát ?", "Thông Báo", MessageBoxButton.OKCancel);
            if (result == MessageBoxResult.OK)
            {
                try
                {
                    while (NavigationService.CanGoBack)
                        NavigationService.RemoveBackEntry();
                }
                catch (InvalidOperationException)
                {
                }
            }
            else
            {
                e.Cancel = true;
            }

        }


        //ham do du lieu vao IsolatedStorageFile
        public static void MoveReferenceDatabase()
        {

            // Obtain the virtual store for the application.
            IsolatedStorageFile iso = IsolatedStorageFile.GetUserStoreForApplication();

            // Create a stream for the file in the installation folder.
            using (Stream input = Application.GetResourceStream(new Uri("CSDLTruyen.sdf", UriKind.Relative)).Stream)
            {
                // Create a stream for the new file in isolated storage.
                using (IsolatedStorageFileStream output = iso.CreateFile("CSDLTruyen.sdf"))
                {
                    // Initialize the buffer.
                    byte[] readBuffer = new byte[4096];
                    int bytesRead = -1;

                    // Copy the file from the installation folder to isolated storage. 
                    while ((bytesRead = input.Read(readBuffer, 0, readBuffer.Length)) > 0)
                    {
                        output.Write(readBuffer, 0, bytesRead);
                    }
                }
            }
        }
    }
}